package com.haojiang.mh.ui.activity

import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.haojiang.mh.R
import com.haojiang.mh.ServerEnum
import com.haojiang.mh.cache.Const
import com.haojiang.mh.ui.base.BaseActivity
import com.haojiang.mh.utils.SpUtil
import kotlinx.android.synthetic.main.activity_choose_service.*
import kotlinx.android.synthetic.main.activity_common_title.*

/**
 * Created by wangzhan on 2019-07-03 11:55
 */

class ChooseWebServiceActivity : BaseActivity() {
    private var server: String = ""

    override fun getTitleName(): String? = ""

    override fun getLayoutId(): Int {
        return R.layout.activity_choose_service
    }


    override fun initActivity() {
        if (isFirstLauncher()){
            aiv_activity_back.visibility= View.GONE
        }
    }

    override fun initView() {
        setCheckedItem()
        tv_china.setOnClickListener(clickListener)
        tv_germany.setOnClickListener(clickListener)
        tv_americ.setOnClickListener(clickListener)
        btn_start.setOnClickListener(clickListener)

    }

    private fun setCheckedItem() {
        when (SpUtil.get("server", ServerEnum.CHINA.name)) {
            ServerEnum.CHINA.name -> {
                setCheckedText(tv_china)
            }
            ServerEnum.AMERICA.name -> {
                setCheckedText(tv_americ)
            }
            ServerEnum.GERMANY.name -> {
                setCheckedText(tv_germany)
            }
        }

    }

    var clickListener = View.OnClickListener {
        when (it.id) {
            R.id.tv_china -> {
                server = ServerEnum.CHINA.name
                Const.server = ServerEnum.CHINA.url
                setCheckedText(tv_china)
                setUnCheckedText(tv_germany)
                setUnCheckedText(tv_americ)
            }
            R.id.tv_germany -> {
                server = ServerEnum.GERMANY.name
                Const.server = ServerEnum.GERMANY.url
                setCheckedText(tv_germany)
                setUnCheckedText(tv_china)
                setUnCheckedText(tv_americ)
            }
            R.id.tv_americ -> {
                server = ServerEnum.AMERICA.name
                Const.server = ServerEnum.AMERICA.url
                setCheckedText(tv_americ)
                setUnCheckedText(tv_germany)
                setUnCheckedText(tv_china)
            }
            R.id.btn_start -> {
                if (TextUtils.isEmpty(Const.server)) {
                    showToast(R.string.choose_server)
                    return@OnClickListener
                }
                SpUtil.put(Const.IS_FIRST_LAUNCHER, false)
                SpUtil.put("server", parseValue(Const.server))
                redirectNoParamAndFinish(MainActivity::class.java)
            }
        }
    }

    private fun setCheckedText(tv: TextView) {
        tv.setTextColor(resources.getColor(R.color.yellow_end))
        tv.setTextSize(20f)
    }

    private fun setUnCheckedText(tv: TextView) {
        tv.setTextColor(resources.getColor(R.color.deep_grey))
        tv.setTextSize(18f)
    }

    private fun parseValue(url: String): String {
        if (TextUtils.isEmpty(url))
            return ServerEnum.CHINA.name
        for (item in enumValues<ServerEnum>()) {
            if (url.equals(item.url))
                return item.name
        }
        return ServerEnum.CHINA.name
    }
}